<template>
	<el-dialog v-model="visible" :title="!dataForm.scenicId ? '新增' : '修改'" :close-on-click-modal="false" draggable>
  <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" :label-width="100">
  <el-form-item label="车辆类型" prop="centerControlId">
      <el-input v-model="dataForm.type" placeholder="车辆类型"></el-input>
  </el-form-item>
  <el-form-item label="选择景区" prop="centerControlId">
      <el-input v-model="dataForm.scenicId" placeholder="车辆类型"></el-input>
  </el-form-item>
  <el-form-item label="中控ID" prop="centerControlId">
      <el-input v-model="dataForm.centerControlId" placeholder="中控ID"></el-input>
  </el-form-item>
  <el-form-item label="车辆ID" prop="carCode">
      <el-input v-model="dataForm.carId" placeholder="车辆ID"></el-input>
  </el-form-item>
  <el-form-item label="屏幕编号" prop="carCode">
      <el-input v-model="dataForm.screenId" placeholder="屏幕编号"></el-input>
  </el-form-item>
  <el-form-item label="车辆编码" prop="carCode">
      <el-input v-model="dataForm.carCode" placeholder="车辆编码"></el-input>
  </el-form-item>
  <el-form-item label="RTK设备号" prop="scenicId">
      <el-input v-model="dataForm.RTK" placeholder="RTK设备号"></el-input>
  </el-form-item>
  <el-form-item label="控制器编号" prop="scenicId">
      <el-input v-model="dataForm.controllerId" placeholder="控制器编号"></el-input>
  </el-form-item>
  </el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useSsCarApi, useSsCarSubmitApi } from '@/api/order/car'

const emit = defineEmits(['refreshDataList'])

const visible = defineModel<boolean>('visible')
const dataFormRef = ref()

const dataForm = reactive({
	type: '',
	scenicId: '',
	centerControlId: '',
	carId: '',
	screenId: '',
	carCode: '',
	RTK: '',
	controllerId: ''})

const init = (id?: number) => {
	if (id) {
		getCar(id)
	}
}

const getCar = (id: number) => {
	useSsCarApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	type: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	scenicId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	centerControlId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	carId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	screenId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	carCode: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	RTK: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	controllerId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) {
			return false
		}


		useSsCarSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
